package com.chenl.provider.DEMOS.缓存预热;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class InitCache implements CommandLineRunner {
    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        //获取到所有需要初始化的缓存的父类，然后遍历它的子类，执行具体的方法进行缓存的初始化预热
        Map<String, AbstractCache> beansOfType = applicationContext.getBeansOfType(AbstractCache.class);
        if (ObjectUtils.isNotEmpty(beansOfType)) {
            for (Map.Entry<String, AbstractCache> entry : beansOfType.entrySet()) {
                AbstractCache bean = entry.getValue();
                bean.initCache();
            }
        }

    }
}
